package com.drakulo.games.ais.core.tech;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.drakulo.games.ais.core.Resource;
import com.drakulo.games.ais.core.Settings;

/**
 * Represents a technology leaf in the reserch tree.
 * 
 * @author Drakulo
 * 
 */
public class Technology {
	/** The technology ID (for storage) */
	private String id;
	/**
	 * This list contains all parent technologies the player have to own before
	 * starting research for this one
	 */
	private List<Technology> parents;
	/**
	 * This list contains all child technologies. Those technologies won't be
	 * researchable until the current technology is researched
	 */
	private List<Technology> children;
	/** True if the player researched this technology */
	private boolean owned;
	/** The technology name */
	private String name;
	/** The technology description */
	private String description;
	/** The image resource key */
	private String imageKey;
	/** Cost map */
	private Map<Resource, BigDecimal> cost;
	/** The technology level in the tree */
	private int level;

	/**
	 * Constructor
	 */
	public Technology() {
		this.parents = new ArrayList<Technology>();
		this.children = new ArrayList<Technology>();
		level = -1;
	}

	public List<Technology> getParents() {
		return this.parents;
	}

	public void setParents(List<Technology> parents) {
		this.parents = parents;
	}

	public boolean isOwned() {
		if(Settings.ALL_TECHNOLOGIES){
			return true;
		}
		return this.owned;
	}

	public void setOwned(boolean owned) {
		this.owned = owned;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getId() {
		return this.id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<Technology> getChildren() {
		return this.children;
	}

	public void setChildren(List<Technology> children) {
		this.children = children;
	}

	public void addParent(Technology t) {
		this.parents.add(t);
	}

	public void addChild(Technology t) {
		this.children.add(t);
	}

	public String getImageKey() {
		return this.imageKey;
	}

	public void setImageKey(String imageKey) {
		this.imageKey = imageKey;
	}

	public Map<Resource, BigDecimal> getCost() {
		return this.cost;
	}

	public void setCost(Map<Resource, BigDecimal> cost) {
		this.cost = cost;
	}

	/**
	 * Tests if the technology is researchable by checking if all parents have
	 * already been researched
	 * 
	 * @return true of false
	 */
	public boolean isResearchable() {
		if (this.isOwned()) {
			// The technology is already owned
			return false;
		}
		for (Technology t : this.parents) {
			if (!t.isOwned()) {
				// One of the parents is not researched yet.
				return false;
			}
		}
		return true;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}
}
